<?php
declare (strict_types = 1);

namespace app\controller;

use app\model\Test83s;
use QL\QueryList;
use think\Request;

class Test83
{
    /**
     * 使用Querylist采集
     * 将采集的数据添加至数据库
     */
    public function getFiction(){
        $data = QueryList::get('https://www.17k.com/all/book/2_0_0_0_0_0_0_0_4.html')
            ->rules([
                "title"   => ['.bg0 .td3 span .jt,.bg1 .td3 span .jt','text'],
                "newest"  => ['.bg0 .td4 a,.bg1 .td4 a','text'],
                "words"   => ['.bg0 .td5,.bg1 .td5','text'],
                "author"  => ['.bg0 .td6 a,.bg1 .td6 a','text'],
                "uptime"  => ['.bg0 .td7,.bg1 .td7','text'],
                "classify"=> ['.bg0 .td2 a,.bg1 .td2 a','text']
            ])
            ->queryData();
        $res = Test83s::addAll($data);
        if($res){
            echo '添加成功';
        }else{
            echo '添加失败';
        }
    }

    /**
     * @param Request $request
     * 获取全部数据的展示方法
     * 接收搜索条件添加入where数组
     */
    public function show(Request $request){
        if($request['title'] || $request['author'] || $request['classify'] != ""){
            $pre = '/^{4e00}-{9fa5}/u';
            if(!preg_match($pre,$request['title'])){
//                echo '匹配失败';
            }
        }
        $where = [];
        if($request['title']){
            $where[] = ['title','like',"%$request[title]%"];
        }
        if($request['author']){
            $where[] = ['author','=',"$request[author]"];
        }
        if($request['classify']){
            $where[] = ['classify','=',"$request[classify]"];
        }
        $data = Test83s::getAll($where);
        return view('./test83show',['data'=>$data]);
    }

    /**
     * @param Request $request
     * 删除数据的方法
     * 删除条件id不能为非整数，不能小于0
     */
    public function del(Request $request){
        $where['id'] = $request['id'];
        if(!is_numeric($where['id'])) {
            echo "<script>alert('删除条件不能为非整数')</script>";
            header("refresh:0;url=show");
            die();
        }
        if($where['id'] <= 0){
            echo "<script>alert('删除条件不能小于0')</script>";
            header("refresh:0;url=show");
            die();
        }
        $res = Test83s::delOne($where);
        if($res){
            echo "<script>alert('删除成功')</script>";
            header("refresh:0;url=show");
        }else{
            echo "<script>alert('删除失败')</script>";
            header("refresh:0;url=show");
        }
    }

    /**
     * @param Request $request
     * @return \think\response\View
     * 修改页面
     * 接收id，获取数据，返回渲染视图层
     */
    public function up(Request $request){
        $where['id'] = $request['id'];
        $data = Test83s::getOne($where);
        return view('./test83up',['data'=>$data]);
    }

    /**
     * @param Request $request
     * 修改数据的方法
     * 接收数据，调用模型层修改数据方法
     * 输出结果提示
     */
    public function updo(Request $request){
        $where['id']      =  $request['id'];
        $data['title']    =  $request['title'];
        $data['author']   =  $request['author'];
        $data['classify'] =  $request['classify'];
        $data['words']    =  $request['words'];
        $data['newest']   =  $request['newest'];
        $res = Test83s::upOne($where,$data);
        if($res){
            echo "<script>alert('修改成功')</script>";
            header("refresh:0;url=show");
        }else{
            echo "<script>alert('修改失败')</script>";
            header("refresh:0;url=show");
        }
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
